package com.guguskins.back.service.impl;
import com.guguskins.back.mapper.GuAccountMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guguskins.back.common.exception.GlobalMsgException;
import com.guguskins.back.entity.GuAccount;
import com.guguskins.back.service.GuAccountService;
import com.guguskins.back.service.GuStockService;
import com.guguskins.back.utils.steam.Bot.SteamBotResponse;
import com.guguskins.back.utils.steam.SteamBotUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-07-29
 */
@Service
@Slf4j
public class GuAccountServiceImpl extends ServiceImpl<GuAccountMapper, GuAccount> implements GuAccountService {


    @Autowired
    private GuStockService stockService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean backAdd(GuAccount account) {
        if (this.count(new LambdaQueryWrapper<GuAccount>() {
            {
                eq(GuAccount::getAccountNumber, account.getAccountNumber());
            }
        }) > 0) {
            throw new GlobalMsgException("账号已存在");
        }

        //添加数据
        if (account.insert()) {
            //通知海盗海
            SteamBotResponse steamBotResponse = SteamBotUtil.accountAdd(account.getAccountNumber(), account.getAccountPassword(), account.getAccountSharedSecret(), account.getAccountIdentitySecret());
            if (!steamBotResponse.getCode().equals(0)) {
                log.error("海盗海添加账号失败：{}", steamBotResponse.getMessage());
                throw new GlobalMsgException("添加账号失败");
            }
            stockService.getStock(account.getAccountId(), account.getAccountNumber(), account.getAccountSteamId());
            return true;
        }
        return false;
    }

    @Override
    public boolean backEdit(GuAccount account) {
        //添加数据
        if (account.updateById()) {
            if (account.getAccountState().equals(1)) {
                //通知海盗海
                SteamBotResponse steamBotResponse = SteamBotUtil.accountAdd(account.getAccountNumber(), account.getAccountPassword(), account.getAccountSharedSecret(), account.getAccountIdentitySecret());
                if (!steamBotResponse.getCode().equals(0)) {
                    log.error("海盗海添加账号失败：{}", steamBotResponse.getMessage());
                    throw new GlobalMsgException("更新账号失败");
                }
            }
            return true;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean backState(GuAccount account) {
        //修改状态
        GuAccount guAccount = this.getById(account.getAccountId());
        if (ObjectUtils.isEmpty(guAccount)) {
            return false;
        }
        guAccount.setAccountState(account.getAccountState());
        if (guAccount.updateById()) {

            if (guAccount.getAccountState().equals(1)) {
                SteamBotResponse steamBotResponse = SteamBotUtil.accountAdd(guAccount.getAccountNumber(), guAccount.getAccountPassword(), guAccount.getAccountSharedSecret(), guAccount.getAccountIdentitySecret());
                if (!steamBotResponse.getCode().equals(0)) {
                    log.error("海盗海添加账号失败：{}", steamBotResponse.getMessage());
                    throw new GlobalMsgException("更新账号失败");
                }
            } else {
                SteamBotResponse steamBotResponse = SteamBotUtil.accountRemove(guAccount.getAccountNumber());
                if (!steamBotResponse.getCode().equals(0)) {
                    log.error("海盗海移除账号失败：{}", steamBotResponse.getMessage());
                    throw new GlobalMsgException("更新账号失败");
                }
            }
            return true;
        }
        return false;
    }
}
